package controllers;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import models.Customer;
import play.db.jpa.Transactional;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import util.DAO;
import util.PasswordHash;

public class UserCtrl extends Controller {

	public static Result recoverPassword() {
		return ok();
	}

	public static Result login() {

		return ok(request().body().asJson().toString());
	}

	@Transactional
	public static Result newCustomer() throws NoSuchAlgorithmException,
			InvalidKeySpecException {

		Customer s = Json.fromJson(request().body().asJson(), Customer.class);
		s.setPassword(PasswordHash.createHash(s.getPassword()));
		DAO.save(s);
		return ok();
	}

}
